<?php

namespace services\models;

use Yii;

/**
 * This is the model class for table "product_stock".
 *
 * @property string $product_stock_id
 * @property integer $goods_id
 * @property integer $product_id
 * @property integer $stock
 */
class ProductStock extends \common\models\ProductStock
{
    public static function getDb() {
        return \Yii::$app->db;
    }

    public static function getRedis() {
        return \Yii::$app->redis;
    }

    /**
     * 通过SKU ID获取SKU库存信息
     * @param int $product_id SKU ID
     * @return array SKU库存信息
     */
    public static function findByProductId($product_id) {
        $var = self::find()->where('product_id=:product_id', [':product_id' => $product_id])->one();
        return !empty($var) ? \yii\helpers\ArrayHelper::toArray($var) : [];
    }

    /**
     * 通过SKU ID获取SKU库存信息
     * @param int $product_id SKU ID
     * @return array SKU库存信息
     */
    public static function findByProductIdFromRedis($product_id) {
        return intval(self::getRedis()->get('p_stock_' . $product_id));
    }

    /**
     * 通过商品ID获取SKU库存信息
     * @param int $goods_id 商品ID
     * @return array SKU库存信息
     */
    public static function findByGoodsId($goods_id) {
        $vars = self::find()->where('goods_id=:goods_id', [':goods_id' => $goods_id])->all();
        return \yii\helpers\ArrayHelper::toArray($vars);
    }

    /**
     * 通过SKU ID增加数量，更新数据库。
     * @param int $product_id SKU ID
     * @param int $num 数量
     * @return int 1 | 0
     */
    public static function incrByDb($product_id, $num) {
        $sql = 'update ' . self::tableName() . ' set stock=stock+:stock,sale_num=sale_num-:stock where product_id=:product_id';
        $count = self::getDb()->createCommand($sql, [':product_id' => $product_id, ':stock' => $num])->query()->getRowCount();
        return $count;
    }

    /**
     * 通过SKU ID增加数量，更新Redis。
     * @param int $product_id SKU ID
     * @param int $num 数量
     * @return int 1 | 0
     */
    public static function incrByRedis($product_id, $num) {
        return self::getRedis()->incrby('p_stock_' . $product_id, $num);
    }

    /**
     * 通过SKU ID减少数量，更新数据库。
     * @param int $product_id SKU ID
     * @param int $num 数量
     * @return int 1 | 0
     */
    public static function decrByDb($product_id, $num) {
        $sql = 'update ' . self::tableName() . ' set stock=stock-:stock,sale_num=sale_num+:stock where product_id=:product_id and stock>0';
        $count = self::getDb()->createCommand($sql, [':product_id' => $product_id, ':stock' => $num])->query()->getRowCount();
        return $count;
    }

    /**
     * 通过SKU ID减少数量，更新Redis。
     * @param int $product_id SKU ID
     * @param int $num 数量
     * @return int 1 | 0
     */
    public static function decrByRedis($product_id, $num) {
        return self::getRedis()->decrby('p_stock_' . $product_id, $num);
    }

    /**
     * 更新SKU库存数量到Redis
     * @param int $product_id SKU ID
     * @param int $num 库存数量
     * @return bool true | false
     */
    public static function updateStockToRedis($product_id, $num) {
        return self::getRedis()->set('p_stock_' . $product_id, $num);
    }
}
